package com.zhaohang.cn.mybasicactivity;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhaohang.cn.constant.ConstantUtils;
import com.zhaohang.cn.utils.Util;

import java.util.List;

/**
 * 作者: 赵航
 * 创建时间: 2018/10/30 21:22
 * 描述:
 */
public abstract class BasicFragment extends Fragment {


    private LinearLayout mActionBar;
    private LinearLayout mHandLine;
    private LinearLayout mContent;
    private RelativeLayout mLeft;
    private TextView mTitletext;
    private RelativeLayout mRight;
    private LayoutInflater mInflater;
    private View mView;
    private View mChild;
    private SmartRefreshLayout mSmartRefreshLayout;
    private ViewGroup mNoData;
    private View mLoadView;
    private View mErrorView;
    private int isShow;
    private View mBasicView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        mBasicView = inflater.inflate(R.layout.fragment_basic, null);

        mActionBar=(LinearLayout) mBasicView.findViewById(R.id.fragment_actionbar);
        mHandLine=(LinearLayout) mBasicView.findViewById(R.id.fragemt_handlinear);
        mContent=(LinearLayout) mBasicView.findViewById(R.id.fragment_content_layout);

        mLeft = (RelativeLayout) mBasicView.findViewById(R.id.left);
        mTitletext = (TextView) mBasicView.findViewById(R.id.titletext);
        mRight = (RelativeLayout) mBasicView.findViewById(R.id.right);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, CompontApplayCation.mStatbarHight);
        mActionBar.setLayoutParams(params);

        showFragmetState(setShowState());
        return mBasicView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }


    private void initView() {
        mInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = mInflater.inflate(setFragmetSuccessView(), null);
        LinearLayout.LayoutParams  mLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        mContent.addView(mView, mLayoutParams);

        initFragmentView(mView);

        if (!Util.netConnected(getContext())){
            ViewGroup noNet= (ViewGroup)mView.findViewById(noNetView());
            if (noNet==null){
                return;
            }else {

                for (int i = 0; i < noNet.getChildCount(); i++) {
                    noNet.getChildAt(i).setVisibility(View.GONE);
                }

                if (mChild==null) {
                    LinearLayout.LayoutParams  layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                            LinearLayout.LayoutParams.MATCH_PARENT);
                    mChild = mInflater.inflate(R.layout.nonet_layout, null);
                    noNet.addView(mChild,layoutParams);
                }
                initNoNetView(mChild, noNet);
            }

        }else {
            showViewWitheData();

        }

    }

    private void initNoNetView(View child, final ViewGroup noNet) {
        mSmartRefreshLayout = (SmartRefreshLayout) mChild.findViewById(R.id.refreshLayout);

        //下拉刷新
        mSmartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                //                mSmartRefreshLayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
                if (!Util.netConnected(getContext())) {
                    mSmartRefreshLayout.finishRefresh();
                } else {
                    for (int i = 0; i < noNet.getChildCount(); i++) {
                        noNet.getChildAt(i).setVisibility(View.VISIBLE);
                    }
                    mChild.setVisibility(View.GONE);
                    mSmartRefreshLayout.finishRefresh();
                    showViewWitheData();
                }
            }
        });
    }

    private void showViewWitheData() {

        mNoData = mView.findViewById(setNoDataView());
        if (mNoData ==null){
            return;
        }else {
            //加载中的布局
            if (mLoadView == null) {
                LinearLayout.LayoutParams  layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.MATCH_PARENT);
                mLoadView = mInflater.inflate(R.layout.page_loading, null);
                mNoData.addView(mLoadView,0,layoutParams);
            }
            if (mErrorView == null) {
                LinearLayout.LayoutParams  layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.MATCH_PARENT);
                mErrorView =  mInflater.inflate(R.layout.page_error, null);
                mNoData.addView(mLoadView,1,layoutParams);
            }

            isShow = 0;

            //根据是否有数据来展示View
            showDataPager();


            changeView();
        }
    }

    private void changeView() {
        //去网络获取数据
        new Thread(new Runnable() {
            @Override
            public void run() {
                //谁用谁传
                Object obj = getNetData();
                //判断数据,切换状态
                isShow = checkData(obj);
                Util.runOnUIThread(new Runnable() {
                    @Override
                    public void run() {
                        //根据状态切换UI
                        showDataPager();
                    }
                });


            }
        }).start();

    }

    private int checkData(Object obj) {
        //如果当前的对象为空,那么肯定是错误界面
        if (obj == null) {
            return 1;
        } else {
            //这里返回就是成功了吗?
            //如果走到这里有两种情况,一个是bean对象,一个集合,
            //如果是集合,那么如果集合里的数据个数为0,那么也是失败的
            if (obj instanceof List) {
                //说明是集合
                List list = (List) obj;
                if (list.size() > 0) {
                    //说明有数据
                    return 2;
                } else {
                    //说明没有数据
                    return 1;
                }
            } else {
                //不是集合,是对象
                return 2;
            }
        }
    }

    private Object getNetData() {

        return "";
    }

    private void showDataPager() {
        for (int i = 0; i < mNoData.getChildCount(); i++) {
            mNoData.getChildAt(i).setVisibility(View.GONE);
        }
        switch (isShow){
            case 0:
                mLoadView.setVisibility(View.VISIBLE);
                break;
            case 1:
                mErrorView.setVisibility(View.VISIBLE);
                break;
            case 2:
                for (int i = 2; i < mNoData.getChildCount(); i++) {
                    mNoData.getChildAt(i).setVisibility(View.VISIBLE);
                }
                break;
        }

    }

    /**
     *
     * @return 展示是否有数据的view
     */
    protected int setNoDataView() {

        return 0;
    }


    protected int noNetView() {
        return 0;
    }

    /**
     * 初始化view控件
     */
    protected abstract void initFragmentView(View mView);

    /**
     *
     * @return 初始化加载布局
     */
    protected abstract int setFragmetSuccessView();


    private void showFragmetState(int showStyle) {
        switch (showStyle) {
            case ConstantUtils.LINEARSHOW:
                mActionBar.setVisibility(View.VISIBLE);
                mHandLine.setVisibility(View.VISIBLE);
                break;
            case ConstantUtils.RELEATSHOW:
                mActionBar.setVisibility(View.GONE);
                mHandLine.setVisibility(View.GONE);
                break;
        }
    }

    protected int setShowState() {
        return ConstantUtils.LINEARSHOW;
    }
    /**
     * @return 线性布局的头布局
     */
    protected LinearLayout getHandLinear() {
        if (mHandLine == null) {
            mHandLine=(LinearLayout) mBasicView.findViewById(R.id.fragemt_handlinear);
        }
        return mHandLine;
    }

    /**
     * 获取最顶部导航栏
     */
    protected LinearLayout getActionLinear(){
        if (mActionBar==null){
            mActionBar=(LinearLayout) mBasicView.findViewById(R.id.fragment_actionbar);
        }
        return mActionBar;
    }

    /**
     * 设置左边按钮里是文字 setLeftShow(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
     *
     * @throws
     * @since 1.0.0
     */
    protected void setLeftShow(int tag, int resID) {
        if (null == mLeft) {
            mLeft = (RelativeLayout) mBasicView.findViewById(R.id.left);
        }
        mLeft.removeAllViews();
        mLeft.setVisibility(View.VISIBLE);
        if (tag == 0) {
            View view = getLayoutInflater().inflate(R.layout.textview, null);
            TextView text = view.findViewById(R.id.text);
            text.setText(this.getResources().getString(resID));
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            mLeft.addView(view, params);
        } else if (tag == 1) {
            View view = getLayoutInflater().inflate(R.layout.imageview, null);
            ImageView imageView = view.findViewById(R.id.icon);
            imageView.setImageResource(resID);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

            mLeft.addView(view, params);
        } else if (tag == 3) {
            View view = getLayoutInflater().inflate(R.layout.weightback, null);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

            mLeft.addView(view, params);
        }
    }

    protected void setTitleText(String title) {

        if (mTitletext==null){
            mTitletext = (TextView) mBasicView.findViewById(R.id.titletext);
        }
        mTitletext.setText(title);
    }
    /**
     * 设置右边边按钮里是文字 setRightShow(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
     *
     * @throws
     * @since 1.0.0
     */
    protected void setRightShow(int tag, int resID) {
        if (null == mRight) {
            mRight = (RelativeLayout) mBasicView.findViewById(R.id.right);
        }
        mRight.removeAllViews();
        mRight.setVisibility(View.VISIBLE);
        if (tag == 0) {
            View view = getLayoutInflater().inflate(R.layout.textview, null);
            TextView text = view.findViewById(R.id.text);
            text.setText(this.getResources().getString(resID));
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            mRight.addView(view, params);
        } else if (tag == 1) {
            View view = getLayoutInflater().inflate(R.layout.imageview, null);
            ImageView imageView = view.findViewById(R.id.icon);
            imageView.setImageResource(resID);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            mRight.addView(view, params);
        }
    }

}
